/* DialogEditStep.java
 * 
 * Copyright (C) 2004 Paolo Casarini <paolo@casarini.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package org.casarini.prbm.gui.dialog;

import java.awt.*;

import org.casarini.prbm.gui.PRB;
import org.casarini.prbm.gui.component.CTextField;
import org.casarini.prbm.model.Passo;

public class DialogEditStep extends DefaultDialogEdit
{
	private static final long serialVersionUID = 6494229960823756833L;

	Passo p;
    PRB parent;

	public DialogEditStep(PRB parent, Passo p)
	{
		super(parent, true);
		this.parent=parent;
		this.p=p;
       	setBackground(Color.lightGray);

		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		setLayout(null);
		setVisible(false);
		setSize(getInsets().left + getInsets().right + 191,getInsets().top + getInsets().bottom + 135);
		val_azimut = new CTextField(CTextField.CTF_NUM,3);
		val_azimut.setBounds(getInsets().left + 114,getInsets().top + 6,60,18);
		add(val_azimut);
		label1 = new java.awt.Label("Azimut :",Label.RIGHT);
		label1.setBounds(getInsets().left + 0,getInsets().top + 12,108,12);
		label1.setFont(new Font("Dialog", Font.PLAIN, 12));
		add(label1);
		label2 = new java.awt.Label("Metri percorsi :",Label.RIGHT);
		label2.setBounds(getInsets().left + 0,getInsets().top + 36,108,12);
		label2.setFont(new Font("Dialog", Font.PLAIN, 12));
		add(label2);
		label3 = new java.awt.Label("minuti impiegati :",Label.RIGHT);
		label3.setBounds(getInsets().left + 0,getInsets().top + 60,108,12);
		label3.setFont(new Font("Dialog", Font.PLAIN, 12));
		add(label3);
		val_spazio = new CTextField(CTextField.CTF_NUM,6);
		val_spazio.setBounds(getInsets().left + 114,getInsets().top + 30,60,18);
		add(val_spazio);
		val_tempo = new CTextField(CTextField.CTF_NUM,5);
		val_tempo.setBounds(getInsets().left + 114,getInsets().top + 54,60,18);
		add(val_tempo);
		okbutton = new java.awt.Button();
		okbutton.setActionCommand("button");
		okbutton.setLabel("OK");
		okbutton.setBounds(getInsets().left + 22,getInsets().top + 96,60,24);
		okbutton.setFont(new Font("Dialog", Font.BOLD, 12));
		okbutton.setBackground(new Color(12632256));
		add(okbutton);
		okbutton.setEnabled(false);
		cancelbutton = new java.awt.Button();
		cancelbutton.setActionCommand("button");
		cancelbutton.setLabel("Annulla");
		cancelbutton.setBounds(getInsets().left + 106,getInsets().top + 96,62,24);
		cancelbutton.setFont(new Font("Dialog", Font.BOLD, 12));
		cancelbutton.setBackground(new Color(12632256));
		add(cancelbutton);
		setTitle("Dati Traccia");
		//}}

		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		cancelbutton.addActionListener(lSymAction);
		SymKey aSymKey = new SymKey();
		val_azimut.addKeyListener(aSymKey);
		okbutton.addActionListener(lSymAction);
		val_spazio.addKeyListener(aSymKey);
		val_tempo.addKeyListener(aSymKey);
		//}}
		
		val_azimut.setText(""+p.azimut);
		val_spazio.setText(""+p.metri);
		val_tempo.setText(""+p.tempo);
	}
	
	public void addNotify()
	{
  	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the getInsets
		setSize(getInsets().left + getInsets().right + d.width, getInsets().top + getInsets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(getInsets().left, getInsets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;


	//{{DECLARE_CONTROLS
	CTextField val_azimut;
	java.awt.Label label1;
	java.awt.Label label2;
	java.awt.Label label3;
	CTextField val_spazio;
	CTextField val_tempo;
	java.awt.Button okbutton;
	java.awt.Button cancelbutton;
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == cancelbutton)
				cancelbutton_Action(event);
			else if (object == okbutton)
				okbutton_Action(event);
		}
	}

	void cancelbutton_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Hide the Dialog
		setVisible(false);
		dispose();
		//}}
	}

	class SymKey extends java.awt.event.KeyAdapter
	{
		public void keyTyped(java.awt.event.KeyEvent event)
		{
			Object object = event.getSource();
			if (object == val_azimut)
				valAzimut_Typed(event);
			else if (object == val_spazio)
				valSpazio_Typed(event);
			else if (object == val_tempo)
				valTempo_Typed(event);
		}
	}

	void okbutton_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
		int a,m;
		a=Integer.parseInt(val_azimut.getText());
		m=Integer.parseInt(val_spazio.getText());
		if(a<0||a>360)
		{
		    AttentionDialog d=new AttentionDialog(parent,"ATTENZIONE!","L'angolo deve essere tra 0 e 360");
		    d.display();
		}
		else if(m<1)
		{
		    AttentionDialog d=new AttentionDialog(parent,"ATTENZIONE!","Lo spazio percorso deve essere maggiore di un metro");
		    d.display();
		}
		else
		{
    		p.azimut=Integer.parseInt(val_azimut.getText());
	    	p.metri=Integer.parseInt(val_spazio.getText());
		    p.tempo=Integer.parseInt(val_tempo.getText());
            parent.setPrbModified(true);
       		//{{CONNECTION
	    	// Hide the Dialog
		    setVisible(false);
		    dispose();
		    //}}
		}
	}
	void valAzimut_Typed(java.awt.event.KeyEvent event)
	{
		// to do: code goes here.
		
		//{{CONNECTION
		// Enable the Button on condition... Is not enabled?
		okbutton.setEnabled(true);
		//}}
	}


	void valSpazio_Typed(java.awt.event.KeyEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Enable the Button
		okbutton.setEnabled(true);
		//}}
	}

	void valTempo_Typed(java.awt.event.KeyEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Enable the Button
		okbutton.setEnabled(true);
		//}}
	}
}
